package com.easidea.tim.dao.commodity;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springside.core.dao.extend.HibernateEntityExtendDao;

import com.easidea.tim.entity.commodity.Product;

public class ProductDao extends HibernateEntityExtendDao<Product> {

	/**
	 * 根据名字和色号获取对应产品信息
	 * 
	 * @author Harrison
	 * @param name
	 * @param color
	 * @return
	 */
	public Product getProductByColorAndName(String name, String color){
		if(name != null ){
			Criteria cte = createUnDeletedCriteria();
			cte.add(Restrictions.eq("name", name.trim().toUpperCase()));
			cte.add(Restrictions.eq("dummyFlag", false));
			if(StringUtils.isNotBlank(color))cte.add(Restrictions.eq("colorCode", color.trim().toUpperCase()));
			List<Product> products = cte.list();
			if(products != null && products.size() >0){
				return products.get(0);
			}
		}
		return null;
		
	}
	
	/**
	 * 获取底价列表
	 * @param name
	 * @param color
	 * @return
	 */
	public Product getProductBasicPrice(String name){
		if(name != null ){
			Criteria cte = createUnDeletedCriteria();
			cte.add(Restrictions.eq("name", name.trim().toUpperCase()));
			cte.add(Restrictions.eq("dummyFlag", false));
			cte.add(Restrictions.gt("basicPrice", Float.valueOf("0")));
			List<Product> products = cte.list();
			if(products != null && products.size() >0){
				return products.get(0);
			}
		}
		return null;
		
	}
	
	/**
	 * 判断是否存在品名、色号，水准相同的产品
	 * 
	 * @param name
	 * @param internalCode
	 * @param colorCode
	 * @return
	 */

	public Product isExistSame(String name, String standardCode,
			String colorCode) {
		Criteria ca = this.createUnDeletedCriteria();
		ca.add(Restrictions.eq("name", name));
		ca.add(Restrictions.eq("standardCode", standardCode));
		ca.add(Restrictions.eq("colorCode", colorCode));
		return (Product) ca.uniqueResult();
	}
	
	public Product isExistSame(String name,String colorCode) {
		  Criteria ca = this.createUnDeletedCriteria();
		  ca.add(Restrictions.eq("name", name));
		  ca.add(Restrictions.eq("colorCode", colorCode));
		  return (Product)ca.uniqueResult();
		  
	}
	
	public Product isExistSame(String name) {
		  Criteria ca = this.createUnDeletedCriteria();
		  ca.add(Restrictions.eq("name", name));
		  return (Product)ca.list().get(0);
		  
	}
	
	public List<Product> filteProductByName(String name) {
		List<Product> pList = null;
		Criteria cte = createUnDeletedCriteria();
		cte.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		pList = cte.list();
		return pList;
	}
}
